/*
 * LoggerFactory.java
 *
 * $Id: LoggerFactory.java,v 1.1 2008-03-18 12:58:46 mario Exp $
 */
package org.ceteca.explica.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
* Factory class for getting the Logger instance. It initializes and
* configures the logging system.
* <br/>
* Responsibilities:
* <ul>
* 	<li>Configuration and initialization of the logging system. </li>
* </ul>
* <br/>
* @author Mario García García <mario@imagos.es>
* Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <br/>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* <br/>
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
public class LoggerFactory {
	/** 
	 * Returns the unique instance of the Logger class.
	 * @return Logger, unique instance of Logger.
	 */
	public static final Logger getLogger(String category) {
		return Logger.getLogger(category);
	} 
	
	/**
	 * Configures and initializes the logging system with the default values. 
	 * The chosen logging system is Apache Logging4Java library.
	 */
	public static final void initLogger() {
		configLogger(null, (String)null);
	}
	
	/**
	 * Configures and initializes the logging system. The chosen logging 
	 * system is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param configFile String, path to the logging configuration file.
	 */
	public static final void initLogger(String logDir, String configFile) {
		configLogger(logDir, configFile);
	}
	
	/**
	 * Configures and initializes the logging system. The chosen logging 
	 * system is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param configFile URL, URL that points to the logging configuration file.
	 */
	public static final void initLogger(String logDir, URL configFile) {
		configLogger(logDir, configFile);
	}
	
	/**
	 * Configures and initializes the logging system. The chosen logging 
	 * system is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param isConfigFile InputStream, input stream opened to the logging 
	 * configuration file.
	 */
	public static final void initLogger(String logDir, InputStream isConfigFile) {
		configLogger(logDir, isConfigFile);
	}

	/**
	 * Configures and initializes the logging system. The chosen logging system
	 * is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param configFile String, path to the logging configuration file.
	 */
	private static final void configLogger(String logDir, String configFile) {
		String pathBase = "";
		
		try {
            File currDir = new File(".");
            pathBase = currDir.getCanonicalPath();
        }
        catch (IOException ioEx ) {
            System.out.println ("Error : " + ioEx );
        }
        
		if (logDir == null) {
	        logDir = pathBase + File.separator + "logs" + File.separator;
		} // if no log dir was specified
		
		if (configFile == null) {
			configFile = pathBase + File.separator + "src/org/ceteca/javaglade/log4j.xml"; 
		}
		
		
        System.setProperty("log.directory", logDir);
        
        // Init the log4Java
        System.out.println("Configurando Log4J para JavaGlade: " + configFile);
		DOMConfigurator.configure(configFile);
	}
	
	/**
	 * Configures and initializes the logging system. The chosen logging system
	 * is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param configFile URL, URL to the logging configuration file.
	 */
	private static final void configLogger(String logDir, URL configFile) {
		String pathBase = "";
		
		try {
            File currDir = new File(".");
            pathBase = currDir.getCanonicalPath();
        }
        catch (IOException ioEx ) {
            System.out.println ("Error : " + ioEx );
        }
        
		if (logDir == null) {
	        logDir = pathBase + File.separator + "logs" + File.separator;
		} // if no log dir was specified

		try {
			if (configFile == null) {
				configFile = new URL(pathBase + File.separator + "src/org/ceteca/javaglade/log4j.xml"); 
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
        System.setProperty("log.directory", logDir);
        
        // Init the log4Java
        System.out.println("Configurando Log4J para JavaGlade: " + configFile);
		DOMConfigurator.configure(configFile);
	}
	
	/**
	 * Configures and initializes the logging system. The chosen logging system
	 * is Apache Logging4Java library.
	 * @param logDir String, path to the directory where logs are stored.
	 * @param isConfigFile InputStream, input stream opened to the logging configuration file.
	 */
	private static final void configLogger(String logDir, InputStream isConfigFile) {
		String pathBase = "";
		
		try {
            File currDir = new File(".");
            pathBase = currDir.getCanonicalPath();
        }
        catch (IOException ioEx ) {
            System.out.println ("Error : " + ioEx );
        }
        
		if (logDir == null) {
	        logDir = pathBase + File.separator + "logs" + File.separator;
		} // if no log dir was specified

		try {
			if (isConfigFile == null) {
				isConfigFile = new FileInputStream(new File(pathBase + File.separator + "src/org/ceteca/javaglade/log4j.xml")); 
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		
        System.setProperty("log.directory", logDir);
        
        // Init the log4Java
        System.out.println("Configurando Log4J para JavaGlade: " + isConfigFile);
        
        new DOMConfigurator().doConfigure(isConfigFile, LogManager.getLoggerRepository());
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: LoggerFactory.java,v 1.1 2008-03-18 12:58:46 mario Exp $";
	}
}
